﻿using UnityEditor;
using UnityEngine;
 
public class ScenePhysicsTool : EditorWindow
{

    private int times = 1;
    private float timestep = 0.01f;
    private GameObject obj = null;
    private void OnGUI()
    {
        times = EditorGUILayout.IntField(times);
        timestep = EditorGUILayout.FloatField(timestep);
        obj = EditorGUILayout.ObjectField(obj, typeof(GameObject), true) as GameObject;
        if (GUILayout.Button("Run Physics"))
        {
            StepPhysics();
        }
        if (GUILayout.Button("Init"))
        {
            Physics.autoSimulation = false;
            Physics2D.autoSimulation = false;
        }
        if (GUILayout.Button("ResetVelocity"))
        {
            obj.GetComponent<Rigidbody>().velocity = Vector3.zero;
        }
    }
 
    private void StepPhysics()
    {
        for (int i = 0; i < times; i++)
        {
            Physics.Simulate(timestep);

            Physics2D.Simulate(timestep);
            // var result = Physics2D.OverlapCircle(this.obj.transform.position, 0.5f, 1 << 6);
            // if (result)
            // {
            //     //this.Reset();
            // }

            //Debug.Log(obj.GetComponent<Rigidbody2D>().velocity);
        }
    }

    private void Reset()
    {
        this.obj.GetComponent<Rigidbody>().velocity = Vector2.zero;
        this.obj.transform.position = new Vector3(0, 5, 0);
    }
 
    [MenuItem("Tools/Scene Physics")]
    private static void OpenWindow()
    {
        GetWindow<ScenePhysicsTool>(false, "Physics", true);
    }
}
// 1. 要能检测到小球落入洞中  =>  Physics2D.OverlapCircle √
// 可修改的参数：小球的重力 受到的空气阻力 初速度；可以在每次被撞击后去修改这些参数
